package cn.tedu.charging.device.service.impl;

import cn.tedu.charging.common.pojo.bo.StationInfoBO;
import cn.tedu.charging.common.pojo.po.ChargingGunInfoPO;
import cn.tedu.charging.common.pojo.po.ChargingStationPO;
import cn.tedu.charging.common.pojo.query.NearStationsQuery;
import cn.tedu.charging.common.pojo.vo.GunInfoVO;
import cn.tedu.charging.common.pojo.vo.StationDetailVO;
import cn.tedu.charging.common.pojo.vo.StationInfoVO;
import cn.tedu.charging.device.repository.DeviceRepository;
import cn.tedu.charging.device.service.DeviceService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.GeoResult;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DeviceServiceImpl implements DeviceService {
    @Autowired
    private DeviceRepository deviceRepository;
    @Override
    public List<StationInfoVO> nearStations(NearStationsQuery query) {
        //1.query 中心点 半径 查询命中场站元素 可以获取的查询结果包含
        //stationId(元素值) lng lat(坐标) distance(距离) stationName stationStatus avgPrice=1.27
        List<StationInfoBO> stationBos = deviceRepository.nearStations(query);
        //2.使用仓储层查询封装的bos转化成vos 每个元素就差一个avgPrice
        List<StationInfoVO> vos=null;
        if (stationBos!=null&&stationBos.size()>0){
            //说明命中了数据
            vos=stationBos.stream().map(bo->{
                //bo 直接拷贝成vo
                StationInfoVO vo=new StationInfoVO();
                BeanUtils.copyProperties(bo,vo);
                //单独计算该场站的平均电价
                vo.setAvgPrice(calculateAvgPrice(bo.getStationId()));
                return vo;
            }).collect(Collectors.toList());
        }
        return vos;
    }

    private Double calculateAvgPrice(Integer stationId) {
        //TODO 可以扩展方法 实现查询某个场站平均电价
        return 1.27;
    }

    @Override
    public StationDetailVO detailStation(Integer stationId) {
        //1.查询场站信息
        ChargingStationPO stationPO = deviceRepository.getStationById(stationId);
        //判断非空
        StationDetailVO vo=null;
        if (stationPO!=null){
            vo=new StationDetailVO();
            //2.查询枪列表
            List<ChargingGunInfoPO> gunPos = deviceRepository.getGunListByStationId(stationId);
            List<GunInfoVO> gunVos=null;
            if (gunPos!=null&&gunPos.size()>0){
                gunVos=gunPos.stream().map(gunPo->{
                    GunInfoVO gunVo=new GunInfoVO();
                    BeanUtils.copyProperties(gunPo,gunVo);
                    return gunVo;
                }).collect(Collectors.toList());
            }
            //4.封装完整vo
            vo.setStationId(stationPO.getId());
            vo.setStationName(stationPO.getStationName());
            vo.setAddress(stationPO.getAddress());
            vo.setGunInfoVos(gunVos);
        }
        return vo;
    }

    @Override
    public Object updateGunStatus(Integer gunId, Integer status) {
        //TODO
        return null;
    }
}
